package com.qiyi.bean;

import java.io.File;
import java.text.DecimalFormat;

import android.os.Environment;
import android.os.StatFs;

public final class EastStudy {
    public static final String PLAYING_URI  = "playingUri";

    // Connection timeout
    public static final int CONNECTION_TIMEOUT = 5000;

    // Status
    public static final int STATUS_FAILED     = 0;
    public static final int STATUS_PAUSED     = 1;
    public static final int STATUS_PENDING    = 2;
    public static final int STATUS_RUNNING    = 3;
    public static final int STATUS_SUCCESSFUL = 4;

    // Lesson Type
    public static final int LESSON_TYPE_MP4  = 0;
    public static final int LESSON_TYPE_HTML = 1;

    public interface BaseColumns {
        /**
         * The unique ID for a row.
         * <P>Type: INTEGER (long)</P>
         */
        public static final String COLUMN_ID = "_id";

        /**
         * The name of the table
         * <P>Type: TEXT</P>
         */
        public static final String COLUMN_NAME = "name";

        /**
         * The sort order of the table
         * <P>Type: INTEGER</P>
         */
        public static final String COLUMN_SORT_ORDER = "sort_order";

        /**
         * The default sort order for table
         */
        public static final String DEFAULT_SORT_ORDER = COLUMN_SORT_ORDER + " ASC";

        /**
         * The default query columns
         */
        public static final String COLUMNS[] = { COLUMN_ID, COLUMN_NAME };

        public static final int COLUMN_ID_INDEX   = 0;
        public static final int COLUMN_NAME_INDEX = 1;
    }

//    public interface Grade extends BaseColumns {
//        /**
//         * The table name
//         */
//        public static final String TABLE_NAME = "grade";
//    }
//
//    public interface Subject extends BaseColumns {
//        /**
//         * The table name
//         */
//        public static final String TABLE_NAME = "subject";
//    }

//    public interface Version extends BaseColumns {
//        /**
//         * The table name
//         */
//        public static final String TABLE_NAME = "version";
//    }

//    public interface Join {
//        /**
//         * The table name
//         */
//        public static final String TABLE_NAME = "join_table";
//
//        /**
//         * The unique ID for a row.
//         * <P>Type: INTEGER (long)</P>
//         */
//        public static final String COLUMN_ID = "_id";
//
//        /**
//         * The grade id of the table
//         * <P>Type: INTEGER (long)</P>
//         */
//        public static final String COLUMN_GRADE_ID = "grade_id";
//
//        /**
//         * The subject id of the table
//         * <P>Type: INTEGER (long)</P>
//         */
//        public static final String COLUMN_SUBJECT_ID = "subject_id";
//
//        /**
//         * The version id of the table
//         * <P>Type: INTEGER (long)</P>
//         */
//        //public static final String COLUMN_VERSION_ID = "version_id";
//    }
//
//    public interface Course extends BaseColumns {
//        /**
//         * The table name
//         */
//        public static final String TABLE_NAME = "course";
//
//        /**
//         * The join id of the table
//         * <P>Type: INTEGER (long)</P>
//         */
//        public static final String COLUMN_JOIN_ID = "join_id";
//    }

//    public interface Lesson extends BaseColumns {
//        /**
//         * The table name
//         */
//        public static final String TABLE_NAME = "lesson";
//
//        /**
//         * The url of the table
//         * <P>Type: TEXT</P>
//         */
//        public static final String COLUMN_URL = "url";
//
//        /**
//         * The type of the table
//         * <P>Type: INTEGER</P>
//         */
//        public static final String COLUMN_TYPE = "type";
//
//        /**
//         * The status of the table
//         * <P>Type: INTEGER</P>
//         */
//        public static final String COLUMN_STATUS = "status";
//
//        /**
//         * The course id of the table
//         * <P>Type: INTEGER (long)</P>
//         */
//        public static final String COLUMN_COURSE_ID = "course_id";
//
//        /**
//         * The default query columns
//         */
//        public static final String COLUMNS[] = {
//            COLUMN_ID, COLUMN_NAME, COLUMN_URL, COLUMN_TYPE,
//            COLUMN_STATUS, COLUMN_SORT_ORDER, COLUMN_COURSE_ID
//        };
//
//        public static final int COLUMN_ID_INDEX         = 0;
//        public static final int COLUMN_NAME_INDEX       = 1;
//        public static final int COLUMN_URL_INDEX        = 2;
//        public static final int COLUMN_TYPE_INDEX       = 3;
//        public static final int COLUMN_STATUS_INDEX     = 4;
//        public static final int COLUMN_SORT_ORDER_INDEX = 5;
//        public static final int COLUMN_COURSE_ID_INDEX  = 6;
//    }

    public interface Downloaded {
        /**
         * The table name
         */
        public static final String TABLE_NAME = "downloaded";

        /**
         * The unique ID for a row.
         * <P>Type: INTEGER (long)</P>
         */
        public static final String COLUMN_ID = "_id";
    }

//    private static final StringBuilder where = new StringBuilder();
//    public static final String BASE_WHERE = BaseColumns.COLUMN_ID + " IN (SELECT DISTINCT ";
//    public static final String GRADE_WHERE = BASE_WHERE + Join.COLUMN_GRADE_ID + " FROM " + Join.TABLE_NAME + ");";
//
//    public static String buildSubjectWhere(long gradeId) {
//        where.setLength(0);
//        return where.append(BASE_WHERE).append(Join.COLUMN_SUBJECT_ID).append(" FROM ").append(Join.TABLE_NAME)
//                    .append(" WHERE ").append(Join.COLUMN_GRADE_ID).append('=').append(gradeId).append(");").toString();
//    }

//    public static String buildVersionWhere(long gradeId, long subjectId) {
//        where.setLength(0);
//        return where.append(BASE_WHERE).append(Join.COLUMN_VERSION_ID).append(" FROM ").append(Join.TABLE_NAME)
//                    .append(" WHERE ").append(Join.COLUMN_GRADE_ID).append('=').append(gradeId).append(" AND ")
//                    .append(Join.COLUMN_SUBJECT_ID).append('=').append(subjectId).append(");").toString();
//    }

//    public static String buildCourseWhere(long gradeId, long subjectId/*, long versionId*/) {
//        where.setLength(0);
//        return where.append(Course.COLUMN_JOIN_ID).append(" IN (SELECT DISTINCT ").append(Join.COLUMN_ID)
//                    .append(" FROM ").append(Join.TABLE_NAME).append(" WHERE ").append(Join.COLUMN_GRADE_ID)
//                    .append('=').append(gradeId).append(" AND ").append(Join.COLUMN_SUBJECT_ID)
//                    .append('=').append(subjectId).append(");").toString();
//                    //.append(" AND ").append(Join.COLUMN_VERSION_ID)
//                    //.append('=').append(versionId).append(");").toString();
//    }

//    //add zhaot 5.11
//    public static String buildMyCourseWhere()
//    {
//    	where.setLength(0);
//    	return where.append("SELECT DISTINCT ").append("g.").append(Course.COLUMN_ID).append(",").append("g.").append(Course.COLUMN_NAME)
//    	       .append(" FROM ").append(Lesson.TABLE_NAME).append(" l")
//    	       .append(",").append(Course.TABLE_NAME).append(" c")
//    	       .append(",").append(Join.TABLE_NAME).append(" j")
//    	       .append(",").append(Grade.TABLE_NAME).append(" g ")
//    	       .append(" where ")
//    	       .append(" l.").append(Lesson.COLUMN_COURSE_ID).append('=').append("c.").append(Course.COLUMN_ID)
//    	       .append(" and ").append(" c.").append(Course.COLUMN_JOIN_ID).append('=').append("j.").append(Join.COLUMN_ID)
//    	       .append(" and ").append(" j.").append(Join.COLUMN_GRADE_ID).append('=').append("g.").append(Grade.COLUMN_ID)
//    	       .toString(); 	
//    }

//    public static String buildLessonWhere(long courseId) {
//        where.setLength(0);
//        return where.append(Lesson.COLUMN_COURSE_ID).append('=').append(courseId).toString();
//    }

    public static String formatBytes(long size) {
        DecimalFormat format = (DecimalFormat)DecimalFormat.getInstance();
        format.setMaximumFractionDigits(2);

        return format.format((double)size / 1048576.0) + " MB";
    }

    public static final String STORAGE_PATH;
    public static final String DOWNLOAD_PATH;
    private static final String DOWNLOAD_DIRECTOTY = "eastdownloads";

    public synchronized static final long getExternalStorageFreeSpace() {
        StatFs stat = new StatFs(STORAGE_PATH);
        return (long)stat.getAvailableBlocks() * (long)stat.getBlockSize();
    }

    static {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            STORAGE_PATH = Environment.getExternalStorageDirectory().getPath();
        } else {
            STORAGE_PATH = "/mnt/sdcard";
        }

        DOWNLOAD_PATH = STORAGE_PATH + File.separator + DOWNLOAD_DIRECTOTY;
        File directory = new File(DOWNLOAD_PATH);
        if (!directory.exists()) {
            directory.mkdir();
        }
    }

    private EastStudy() {
    }
}